﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.Common;
using FluentData;

namespace Framework.Data
{
    /// <summary>
    /// 数据库辅助类
    /// </summary>
    public static class DataHelper
    {
        public static readonly string ConnectionString_NewBase =
            ConfigurationManager.ConnectionStrings["NewBase"].ConnectionString;

        /// <summary>
        /// 到指定的数据库服务器里去获取 Base 库的 IDbContext 用于操作数据
        /// </summary>
        /// <param name="databaseType"></param>
        /// <returns></returns>
        public static IDbContext GetBaseDbContext(DbProviderType databaseType)
        {
            IDbContext context;

            switch (databaseType)
            {
                case DbProviderType.MSSQLServer:
                    context = new DbContext().ConnectionString(ConnectionString_NewBase,
                                                               new SqlServerProvider());
                    break;
                case DbProviderType.MySQL:
                    context = new DbContext().ConnectionString(ConnectionString_NewBase,
                                                               new MySqlProvider());
                    break;
                case DbProviderType.Oracle:
                    context = new DbContext().ConnectionString(ConnectionString_NewBase,
                                                               new OracleProvider());
                    break;
                default:
                    context = new DbContext().ConnectionString(ConnectionString_NewBase,
                                                               new SqlServerProvider());
                    break;
            }

            return context;
        }

        /// <summary>
        /// 获取 Base 库的 IDbContext 用于操作数据。此处为 MS SQL Server 数据库
        /// </summary>
        /// <returns></returns>
        public static IDbContext GetBaseDbContext()
        {
            return GetBaseDbContext(DbProviderType.MSSQLServer);
        }
        ///// <summary>
        ///// 根据数据库业务类型获取其分库数据上下文对象
        ///// </summary>
        ///// <param name="hotelId"></param>
        ///// <param name="dbType"></param>
        ///// <returns></returns>
        //public static IDbContext GetHotelDbContext(string hotelId, DatabaseType dbType)
        //{
        //    return ConnectionHelper.GetHotelDbContext(hotelId, dbType);
        //}

        /// <summary>
        /// 根据数据库业务类型获取其分库数据上下文对象(DatabaseType.Hotel)
        /// </summary>
        /// <param name="hotelId"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static IDbContext GetHotelDbContext(string hotelId)
        {
            return ConnectionHelper.GetHotelDbContext(hotelId, DatabaseType.Hotel);
        }
    }
}
